package com.example.findmystuff;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
import android.widget.EditText;
import android.widget.TextView;
import com.database.findmystuff.*;

public class RegisterActivity extends Activity {

	Button registerSubmit, registerCancel;
	EditText mEmail1,mEmail2,mPw1,mPw2;
	TextView submitFeedback;
	//public static DB database = new DB();
	/**
	 * This method is called when the activity is created
	 * @param savedInstanceState a bundle used to save the state that this instance of the activity is in.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		// Show the Up button in the action bar.
		//getActionBar().setDisplayHomeAsUpEnabled(true);
		
		submitFeedback = (TextView) findViewById(R.id.submitFeedback);
		registerSubmit = (Button) findViewById(R.id.submitButton2);
        registerCancel = (Button) findViewById(R.id.cancelButton2);
        mEmail1 = (EditText) findViewById(R.id.emailText);
        mEmail2 = (EditText) findViewById(R.id.confirmemailText);
        mPw1 = (EditText) findViewById(R.id.pwText);
        mPw2 = (EditText) findViewById(R.id.confirmpwText);
         
        
        registerSubmit.setOnClickListener(
        		new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mEmail1.getText().toString().equals(mEmail2.getText().toString())){
							if(mPw1.getText().toString().equals(mPw2.getText().toString())){
								if(MainActivity.database.getUser(mEmail1.getText().toString())!=null){
									submitFeedback.setText("Account already exists!");
								}else{
									//check that the username is unique from any others <<
									//create the account and add it to the database here <<
									submitFeedback.setText("Success, " + mEmail1.getText().toString() + " created!");
									MainActivity.loggedUser=mEmail2.getText().toString();
									User newUser = new User(mEmail2.getText().toString(), mPw2.getText().toString());
									MainActivity.database.createUser(newUser);
									MainActivity.database.saveObject(MainActivity.database);
									//MainActivity.database.loadSerializedObject(MainActivity.database);
									Intent i = new Intent(RegisterActivity.this, HomeActivity.class);
									startActivity(i);
								}
							}else{
								submitFeedback.setText("Error: Passwords Do Not Match!");
							}
						}else{
							submitFeedback.setText("Error: Emails Do Not Match!");
						}
					}
				});
        
        registerCancel.setOnClickListener(
        		new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent i = new Intent(RegisterActivity.this, MainActivity.class);
						startActivity(i);
					}
				});
	}
	/**
	 * Initializes the contents of the Activity's standard options menu.
	 * @param menu the standard options menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}
/**
 * This hook is called whenever an item in your options menu is selected. The default implementation simply returns false to have the normal processing happen.
 * @param item The menu Item selected
 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
